# -*- coding:utf-8 -*-
import copy
from lxml import etree
from docx import Document
from docx.shared import Inches
from math import ceil

html = etree.parse("./web/index.html", etree.HTMLParser())
tr_list = html.xpath("//table[@id='vuln_distribution']/tbody/tr")
n = 1
content_list = []
level_model = {
    "high":"高危",
    "middle":"中危",
    "low":"低危"
}
tmp = {}
for tr in tr_list:
    if n % 2 != 0:
        if tr.xpath("./td/span[@style='color:#E42B00']"):
            level = "high"
        elif tr.xpath("./td/span[@style='color:#AF6100']"):
            level = "middle"
        elif tr.xpath("./td/span[@style='color:#737373']"):
            level = 'low'
        title = "".join(tr.xpath("./td/span/text()")[0].split())
        tmp['title'] = title
        tmp['level'] = level
    elif n % 2 == 0:
        next_tr_list = tr.xpath("./td/table/tr")
        nu = 0
        for tr in next_tr_list:
            name = "".join(tr.xpath("./th/text()")[0].split())
            if name == "受影响主机":
                content = tr.xpath("./td//a/text()")
            elif name == "详细描述":
                content = tr.xpath("./td/text()")
            elif name == "解决办法":
                content = tr.xpath("./td/text()")
            elif name == "CVE编号":
                content = tr.xpath("./td/a/text()")
            tmp[name] = content
        record = copy.deepcopy(tmp)
        content_list.append(record)
        tmp.clear()
    n += 1

doc = Document()
for record in content_list:
    ip_row_nu = ceil(len(record['受影响主机'])/4)
    row_nu = ip_row_nu + 10
    table = doc.add_table(rows=row_nu, cols=4)
    for current_row in range(6):
        a = table.cell(current_row, 0)
        b = table.cell(current_row, 3)
        a.merge(b)
    for current_row in range(6 + ip_row_nu, row_nu, 1):
        a = table.cell(current_row, 0)
        b = table.cell(current_row, 3)
        a.merge(b)
    doc.add_page_break()
    n = 0
    for row in table.rows:
        if n == 0:
            row.cells[0].text = record['title']
        elif n == 1:
            row.cells[0].text = "漏洞等级"
        elif n == 2:
            row.cells[0].text = level_model[record['level']]
        elif n == 3:
            row.cells[0].text = "CVE 编号"
        elif n == 4:
            if "CVE编号" in record.keys():
                row.cells[0].text = record["CVE编号"][0]
            else:
                row.cells[0].text = "暂无"
        elif n == 5:
            row.cells[0].text = "IP 地址"
        elif n == ip_row_nu + 6:
            row.cells[0].text = "漏洞描述"
        elif n == ip_row_nu + 7:
            row.cells[0].text = record['详细描述'][0]
        elif n == ip_row_nu + 8:
            row.cells[0].text = '解决办法'
        elif n == ip_row_nu + 9:
            row.cells[0].text = record['解决办法'][0]
        n += 1
    m = 0
    for row in table.rows[6: row_nu + 5]:
        cell_nu = 0
        for ip in record["受影响主机"][m*4:(m+1)*4]:
            row.cells[cell_nu].text = ip
            cell_nu += 1
        m += 1
    

doc.save("word.docx")